﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class MonBLL
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable SelectDes()
        {
            return DB.MonHocs.Join(
                DB.Tos,
                a=>a.MaTo,
                b=>b.MaTo,
                (a,b)=> new 
                {
                    a.MaMonHoc,
                    a.TenMon,
                    a.HeSo,
                    b.TenTo
                }
                ).OrderByDescending(s => s.MaMonHoc).AsQueryable();
        }
        public string Insert(MonDTO monhoc)
        {
            try
            {
                MonHoc mh = new MonHoc();
                mh.MaMonHoc = (id++).ToString();
                mh.TenMon = monhoc.TenMonHoc;
                mh.HeSo = monhoc.HeSo;
                mh.MaTo = monhoc.MaTo;
                DB.MonHocs.InsertOnSubmit(mh);
                DB.SubmitChanges();
                return "Thanh cong";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string[] GetTenTo()
        {
            string[] data = new string[1000];
            data = (from t in DB.Tos
                    select t.TenTo).ToArray();
            return data;
        }

        public string GetMaTo(string tento)
        {
            string mato = DB.Tos.Where(tk => tk.TenTo == tento).Select(th => th.MaTo).FirstOrDefault().ToString();
            return mato;
        }
        public string GetTenTo(string mato)
        {
            To to= DB.Tos.Where(tk => tk.MaTo == mato).FirstOrDefault();
            return to.TenTo;
        }
        public string Delete()
        {
            try
            {
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Update(MonDTO mon)
        {
            try
            {
                MonHoc mh = DB.MonHocs.Where(s => s.MaMonHoc == mon.MaMonHoc).FirstOrDefault();
                mh.TenMon = mon.TenMonHoc;
                mh.HeSo = mon.HeSo;
                mh.MaTo = mon.MaTo;
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public List<MonDTO> getMonHoc(string manamhoc, string makhoi)
        {
            var list = DB.MonHocs.Where(s=> !(from p in DB.TKBKhois where p.MaNamHoc==manamhoc && p.MaKhoi==makhoi select p.MaMonHoc).Contains(s.MaMonHoc)).AsQueryable();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<MonDTO> listdto = new List<MonDTO>();
            foreach (var mon in list)
            {
                MonDTO k = new MonDTO();
                k.MaMonHoc = mon.MaMonHoc;
                k.TenMonHoc = mon.TenMon;
                listdto.Add(k);
            }
            return listdto;
        }
        public List<MonDTO> SelectAll()
        {
            List<MonHoc> list = DB.MonHocs.ToList<MonHoc>();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<MonDTO> listdto = new List<MonDTO>();
            foreach (MonHoc mon in list)
            {
                MonDTO k = new MonDTO();
                k.MaMonHoc = mon.MaMonHoc;
                k.TenMonHoc = mon.TenMon;
                listdto.Add(k);
            }
            return listdto;
        }
        public bool check(string tenmonhoc)
        {
            var kq = DB.MonHocs.Where(mh=>mh.TenMon==tenmonhoc ).ToList();
            int kt = 0;
            kt = kq.Count();
            if (kt == 0)
                return true;
            else return false;
        }
    }
}
